﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.BarCode
{
    class ISBN
    {
        private static volatile ISBN instance;

        private static object syncRoot = new Object();

        public static ISBN Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new ISBN();
                        }
                    }
                }
                return instance;
            }
        }

        public string GetBarCode(string barcode)
        {
            if (!Pixysoft.NumberCoder.Instance.IsInt64(barcode))
                throw new Exception("EBOOKLANDISBN-1: Numeric Data Only");

            string type = "UNKNOWN";
            if (barcode.Length == 10 || barcode.Length == 9)
            {
                if (barcode.Length == 10) barcode = barcode.Remove(9, 1);
                barcode = "978" + barcode;
                type = "ISBN";
            }//if
            else if (barcode.Length == 12 && barcode.StartsWith("978"))
            {
                type = "BOOKLAND-NOCHECKDIGIT";
            }//else if
            else if (barcode.Length == 13 && barcode.StartsWith("978"))
            {
                type = "BOOKLAND-CHECKDIGIT";
                barcode = barcode.Remove(12, 1);
            }//else if

            //check to see if its an unknown type
            if (type == "UNKNOWN") throw new Exception("EBOOKLANDISBN-2: Invalid input.  Must start with 978 and be length must be 9, 10, 12, 13 characters.");

            return EAN13.Instance.GetBarCode(barcode);
        }

    }
}
